﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ClassCommon
{
    public class RegexHelper
    {
        #region const
        /// <summary>
        /// 手机号码正则表达式
        /// </summary>
        private const string phoneRegx = "^0?(13|14|15|17|18|19)[0-9]{9}$";

        /// <summary>
        /// 邮箱正则
        /// </summary>
        private const string emailRegx = @"\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}";

        /// <summary>
        /// 是否是中文   
        /// </summary>
        private const string chineseWordRegx = "^[\u4e00-\u9fa5]+$";
        /// <summary>
        /// IP地址
        /// </summary>
        private const string ipRegx = @"(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)";
        /// <summary>
        /// 手机验证码正则
        /// </summary>
        private const string isPhoneCode = @"^[1-9]\d{3}$";
        #endregion


        
        #region 手机号码

        /// <summary>
        /// 验证手机号码是否合法
        /// </summary>
        /// <param name="phoneNum"></param>
        /// <returns></returns>
        public static bool isPhoneNum(string phoneNum)
        {
            if (string.IsNullOrEmpty(phoneNum)) return false;

            return Validate(phoneNum, phoneRegx);
        }

        #endregion

        #region 手机验证码
        public static bool isPhoneCodeValidate(string code) { return Validate(code, isPhoneCode); }
        #endregion

        #region 验证邮箱

        public static bool isEmail(string email)
        {
            return Validate(email, emailRegx);
        }

        #endregion

        #region 中文验证
        public static bool isChinese(string str)
        {
            return Validate(str, chineseWordRegx);
        }

        #endregion

        #region IP地址

        public static bool isIp(string ip)
        {
            return Validate(ip, ipRegx);
        }

        #endregion



        #region 验证输入的字符串是否合法
        /// <summary>
        /// 验证输入的字符串是否合法，合法返回true,否则返回false。
        /// </summary>
        /// <param name="strInput">输入的字符串</param>
        /// <param name="strPattern">模式字符串</param>        
        private static bool Validate(string strInput, string strPattern)
        {
            return Regex.IsMatch(strInput, strPattern);
        }


        /// <summary>
        /// 替换指定字符
        /// </summary>
        /// <param name="input"></param>
        /// <param name="strPattern"></param>
        /// <returns></returns>
        private static string Replace(string input, string strPattern)
        {
            if (string.IsNullOrEmpty(input)) { return string.Empty; }

            return Regex.Replace(input, strPattern, m =>
            {
                if (m == null) { return string.Empty; }

                return input.Replace(m.Value, string.Empty);
            });
        }

        #endregion
    }
}
